package com.majun.huaxishe.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.majun.huaxishe.constant.InfoConstant;
import com.majun.huaxishe.constant.StatusConstant;
import com.majun.huaxishe.entity.Theme;
import com.majun.huaxishe.entity.dto.ThemeDTO;
import com.majun.huaxishe.entity.vo.ThemeVO;
import com.majun.huaxishe.exception.BaseException;
import com.majun.huaxishe.mapper.ThemeMapper;
import com.majun.huaxishe.service.IThemeService;
import com.majun.huaxishe.utils.HxsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 主题表 服务实现类
 * </p>
 *
 * @author 马骏
 * @since 2024-02-29
 */
@Service
public class ThemeServiceImpl extends ServiceImpl<ThemeMapper, Theme> implements IThemeService {

    @Autowired
    private ThemeMapper themeMapper;

    @Override
    public List<Theme> queryAll(String type) {
        return lambdaQuery()
                .eq("index".equals(type),Theme::getStatus, StatusConstant.SHOW)
                .orderByAsc(Theme::getOrder)
                .list();
    }

    @Override
    public Page<ThemeVO> queryPage(ThemeDTO themeDTO) {
        return themeMapper.queryPage(new Page<ThemeVO>(themeDTO.getCurrentPage(), themeDTO.getPageSize()));
    }

    @Override
    public void sameAlbumName(Theme theme) {
        String name = theme.getTitle();
        if (HxsUtils.isEmpty(name)){
            return;
        }
        List<Theme> albums = this.lambdaQuery()
                .eq(Theme::getTitle, name)
                //修改的时候不查自己这一条
                .ne(HxsUtils.isNotEmpty(theme.getId()), Theme::getId,theme.getId())
                .list();
        if (HxsUtils.isNotEmpty(albums)){
            throw new BaseException(InfoConstant.SAME_THEME_NAME);
        }
    }
}
